/**
 * Heightmap class. Data is in static memory; use setData to copy data from some source.
 */
#pragma once

#include <stdio.h>
#include <math.h>
#include "FreeImage/FreeImage.h"
#include <float.h>
#include <iostream>

using namespace std;

class Heightmap {
private:
    int h, w;
    float* data;

    float getMaxHeightX(int x, int minY, int maxY) const;
    float getMinHeightX(int x, int minY, int maxY) const;
    float getMaxHeightY(int y, int minX, int maxX) const;
    float getMinHeightY(int y, int minX, int maxX) const;
public:
    Heightmap();
    Heightmap(FIBITMAP* source, float scale);
    ~Heightmap();

    int height() const { return h; }
    int width() const { return w; }

    float& at(int x, int y) const;
    void setData(FIBITMAP* source, float scale);
    void setData(Heightmap& heightmap);
    void deleteData();

    //determines split position for e.g. kd trees. lower score is better
    void getOptSplitX(float& score, int& split, int minX, int maxX, int minY, int maxY) const;
    void getOptSplitY(float& score, int& split, int minX, int maxX, int minY, int maxY) const;
};
